package com.tanhua.dubbo.server.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.LoginLog;
import com.tanhua.dubbo.server.pojo.Users;
import com.tanhua.dubbo.server.vo.UsersVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
public class Users_xuApiImpl implements Users_xuApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询七天内活跃用户
     *
     * @return
     */
    @Override
    public Integer queryActiveUserLastWeek() {
        long currentTimeMillis = System.currentTimeMillis();
        long lastcurrentTimeMillis = currentTimeMillis - 7 * 24 * 60 * 60 * 1000;
        Query query = Query.query(Criteria.where("logTime").gte(lastcurrentTimeMillis).and("logTime").lte(currentTimeMillis));
        List<LoginLog> loginLogs = this.mongoTemplate.find(query, LoginLog.class);
        Set<Long> set = new HashSet<>();
        for (LoginLog loginLog : loginLogs) {
            Long userId = loginLog.getUserId();
            set.add(userId);
        }
        return set.size();
    }

    /**
     * 查询三十天活跃用户
     *
     * @return
     */
    @Override
    public Integer queryActiveUserLastMonth() {
        /*Long currentTimeMillis = System.currentTimeMillis();
        long lastcurrentTimeMillis = currentTimeMillis - 30 * 24 * 60 * 60 * 1000;
        Query query = Query.query(Criteria.where("logTime").gte(lastcurrentTimeMillis).and("logTime").lte(currentTimeMillis));
        List<LoginLog> loginLogs = this.mongoTemplate.find(query, LoginLog.class);
        Set<Long> set = new HashSet<>();
        for (LoginLog loginLog : loginLogs) {
            Long userId = loginLog.getUserId();
            set.add(userId);
        }*/
        return null;
    }

    /**
     * 今日登录次数
     *
     * @return
     */
    @Override
    public Long loginTimesToday() {
        long currentTimeMillis = System.currentTimeMillis();
        Date date = DateUtil.date();
        DateTime dateTime = DateUtil.beginOfDay(date);
        long dateBeginTime = dateTime.getTime();

        Query query =Query.query(Criteria.where("logTime").gte(dateBeginTime).lte(currentTimeMillis));
       return this.mongoTemplate.count(query, LoginLog.class);



    }

    @Override
    public Long queryLoginCountRate() {
       /* long currentTimeMillis = System.currentTimeMillis();
        Date date = DateUtil.date();
        DateTime dateTime = DateUtil.beginOfDay(date);
        long dateBeginTime = dateTime.getTime();
        long lastDateBeginTime = dateBeginTime - 24 * 60 * 60 * 1000;
        Query query = Query.query(Criteria.where("logTime").gte(dateBeginTime).and("logTime").lte(currentTimeMillis));
        Query query2 = Query.query(Criteria.where("logTime").gte(lastDateBeginTime).and("logTime").lte(dateBeginTime));
        long count = this.mongoTemplate.count(query, LoginLog.class);
        long count2 = this.mongoTemplate.count(query2, LoginLog.class);
        return  (count - count2) / count2 * 100;*/
       return null;
    }

    @Override
    public Integer activeUsersToday() {
        Date date = DateUtil.date();
        DateTime dateTime = DateUtil.beginOfDay(date);
        long dateBeginTime = dateTime.getTime();
        Query query = Query.query(Criteria.where("logTime").gte(dateBeginTime));
        List<LoginLog> loginLogs = this.mongoTemplate.find(query, LoginLog.class);
        Set<Long> set = new HashSet<>();
        for (LoginLog loginLog : loginLogs) {
            Long userId = loginLog.getUserId();
            set.add(userId);
        }
        return set.size();
    }

    @Override
    public Integer activeUsersTodayRate() {
        return null;
    }

    @Override
    public Integer useTimePassWeek() {
        return null;
    }

    @Override
    public Integer activeUsersYesterday() {
        return null;
    }

    @Override
    public Integer activeUsersYesterdayRate() {
        return null;
    }

    @Override
    public UsersVo getActiveUser(Date startTime, Date endTime, Date lastStartTime, Date lastEndTime) {
        return null;
    }

    private UsersVo getCustomTime(Date startTime, Date endTime, Date lastStartTime, Date lastEndTime) {

return  null;
    }

    private UsersVo getMonth(Date startTime, Date endTime, Date lastStartTime, Date lastEndTime) {
        return  null;

    }

    private UsersVo getWeek(Date startTime, Date endTime, Date lastStartTime, Date lastEndTime) {
        return null;
    }
}